package com.atguigu.admin.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 默认异常处理原理:
 * 1.捕获到异常信息当前请求结束
 *2.把异常信息封装到dispatchException
 * dispatchException = ex;
 * 3.处理异常:
 *  获取异常的handler方法，处理异常返回ModelAndView
 *  mv = processHandlerException(request, response, handler, exception);
 *  遍历HandlerExceptionResolver解析异常
 *  shouldApplyTo(request, handler)：判断要返回的数据类型
 */
@Slf4j
@Controller
public class FormTestController {

    @GetMapping("/form_layouts")
    public String form_layouts(){
        int a= 10/0;
        return "form/form_layouts";
    }

    /**
     * 文件上传的原理
     * 1.检查请求是否是文件上传请求
     * 	public boolean isMultipart(HttpServletRequest request) {
     * 		return StringUtils.startsWithIgnoreCase(request.getContentType(),
     * 				(this.strictServletCompliance ? MediaType.MULTIPART_FORM_DATA_VALUE : "multipart/"));
     *
     * 	2.
     * @param username
     * @param email
     * @param headerImg
     * @param photos
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("username") String username,
                         @RequestParam("email") String email,
                         @RequestPart("headerImg") MultipartFile headerImg,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
        log.info(email+username+":"+headerImg.getSize()+":"+photos.length);

        //保存上传文件到指定路径
        if (!headerImg.isEmpty()){
            String originalFilename = headerImg.getOriginalFilename();

            headerImg.transferTo(new File(""));
        }

        return "main";
    }
}
